﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using School.Entities;
using School.Common;
using School.Organization;

namespace School.Pages.Organization
{
    public partial class Certificates : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var xx = (IQueryable<int>)Session["privilage"];

                if (xx.Contains(1301) == true)
                {
                    if (!IsPostBack)
                        LstCampuseBind();
                    lblcode.Visible = false;
                    GridView1.DataSource = Certificate.All().Select(x => new { Id = x.Id, Name = x.Name, Description = x.Description, CName = x.ORG_Campus.Name });
                    GridView1.DataBind();

                    long id = 0;

                    if (long.TryParse(Request.QueryString["Id"], out id) && !IsPostBack)
                    {
                        ORG_Certificate cer = Certificate.Single(id);

                        if (cer != null)
                        {
                            TxtName.Text = cer.Name;
                            TxtDescription.Text = cer.Description;
                            TxtCode.Text = cer.Code;
                            LstCampuse.SelectedValue = cer.Campus_Id.ToString();
                        }
                        else
                        {
                            Response.Redirect("Certificates.aspx");
                        }
                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        private void LstCampuseBind()
        {
            LstCampuse.DataSource = Campus.All();
            LstCampuse.DataTextField = "Name";
            LstCampuse.DataValueField = "Id";
            LstCampuse.Items.Insert(0, new ListItem("اختيار", "-1"));
            LstCampuse.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            long id = 0;

            if (long.TryParse(Request.QueryString["Id"], out id))
            {
                Certificate.Update(new ORG_Certificate() { Id = id, Name = TxtName.Text, Description = TxtDescription.Text, Campus_Id = long.Parse(LstCampuse.SelectedValue), Code = TxtCode.Text });
            }
            else
            {
                Certificate.Add(new ORG_Certificate() { Name = TxtName.Text, Description = TxtDescription.Text, Campus_Id = long.Parse(LstCampuse.SelectedValue), Code = TxtCode.Text });
            }

            Response.Redirect("Certificates.aspx");
        }

        protected void LstCampuse_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LstCampuse.SelectedIndex > 0)
            {
                lblcode.Text = "الكود=" + Campus.getCamp_Cod(Convert.ToInt32(LstCampuse.SelectedValue));
                lblcode.Visible = true;
            }

        }
    }
}